package com.stone.elec.rename.executor.impl;

import java.io.File;
import java.nio.charset.Charset;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.io.FileUtil;
import com.stone.elec.rename.config.ReNameConfig;
import com.stone.elec.rename.executor.BaseExecutor;
import com.stone.elec.rename.executor.ExecutorFactory;
import com.stone.elec.rename.utils.FileUtils;

public class RootExecutor implements BaseExecutor {

    private String path = "";

    private ReNameConfig reNameConfig;

    private static RootExecutor rootExecutorExecutor = new RootExecutor();

    public RootExecutor() {
        reNameConfig = ReNameConfig.reNameConfig;
    }

    public static void put() {
        ExecutorFactory.put("root", rootExecutorExecutor);
    }

    @Override
    public void execute() {
        //修改文件夹名称
        //FileUtils.fileDicValidate(reNameConfig.getBasePath() + "\\" + path);

        //修改db
        List<String> fileName = FileUtil.listFileNames(reNameConfig.getBasePath() + "\\" + path);
        //	log.info("获取文件列表 {}", fileName);

        fileName.stream().forEach(v -> {
            //读取file
            File file = FileUtil.file(reNameConfig.getBasePath() + "\\" + path + "\\" + v);
            if (!FileUtil.isDirectory(file)) {
                //先验证文件名
                FileUtils.fileNameValidate(file);

                FileUtils.pomValidate(file);

            }
        });

    }

    private void fileContentValidate(File file) {
        List<String> files = FileUtil.readLines(file, Charset.defaultCharset());

        files = files.stream().map(v -> {
            String content = v;
            if (content.contains(reNameConfig.getOldProjectName())) {
                content = content.replaceAll(reNameConfig.getOldProjectName(), reNameConfig.getProjectName());
            }
            return content;
        }).collect(Collectors.toList());
        //log.info("转换后的文件内容为 {}", files);
        //重新写入文件
        FileUtil.writeLines(files, file, Charset.defaultCharset(), false);

        //Dockerfile.MAINTAINER,2elecx.sql.pig todo

    }

}
